﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Data;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Data;
using ScrumTable.Common.Excel.OpenXmlSdk;
using ScrumTable.Common.Excel.OpenXmlSdk.Extensions;

#endregion 

namespace ScrumTable.Common.Excel.Test
{
    /// <summary>
    /// This is the test class for WorkSheet class.
    /// </summary>
    [TestClass]
    public class WorksheetTest
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        
        private DataTable ToTest { get; set; }

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #endregion

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new WorksheetTest instance.
        /// </summary>
        public WorksheetTest()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Use TestInitialize to run code before running each test .
        /// </summary>
        [TestInitialize]
        public void TestInitialize()
        {
            IWorkbookSerializer workbook = new OpenXmlWorkbookSerializer(Resources.GetProjectTestDataFilePath(TestContext.TestDir));
            ToTest = workbook.Deserialize().Tables[Resources.ThirdWorksheetName];
        }

        [TestMethod]
        public void TestColumnDefinitions()
        {
            Assert.IsNotNull(ToTest);
            Assert.AreEqual(Resources.ThridSheetHeaderNames.Length, ToTest.Columns.Count);

            for (int i = 0; i < Resources.ThridSheetHeaderNames.Length; ++i)
            {
                if (Resources.ThridSheetHeaderNames[i] == null) { continue; }

                Assert.AreEqual(Resources.ThridSheetHeaderNames[i], ToTest.Columns[Resources.ThridSheetHeaderNames[i]].ColumnName);
                Assert.AreEqual(Resources.ThirdSheetColumnIndexes[i], ToTest.Columns[Resources.ThridSheetHeaderNames[i]].Ordinal);
                Assert.AreEqual(typeof(DataValue), ToTest.Columns[Resources.ThridSheetHeaderNames[i]].DataType);
            }
        }

        [TestMethod]
        public void TestSearchForRow()
        {
            Assert.IsNotNull(ToTest.SearchForRow("1002"));
        }

        [TestMethod]
        public void TestContentRows()
        {
            Assert.AreEqual(1000, ToTest.GetRows().First().GetFirstPrimaryKey<int>());
            Assert.AreEqual(1001, ToTest.GetRows().Second().GetFirstPrimaryKey<int>());
            Assert.AreEqual(1002, ToTest.GetRows().Third().GetFirstPrimaryKey<int>());
            Assert.AreEqual(1004, ToTest.GetRows().Fourth().GetFirstPrimaryKey<int>());
            Assert.AreEqual(4, ToTest.GetRows().Count());
        }

        [TestMethod]
        public void TestTryGetRowById()
        {
            DataRow element;
            bool elementFound = ToTest.TryGetRowById(1004, out element);

            Assert.IsNotNull(element);
            Assert.IsTrue(elementFound);
            Assert.AreEqual(1004, element.GetFirstPrimaryKey<int>());
        }


        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}